package com.example.administrator.text;

import android.content.Intent;
import android.os.Bundle;
import com.uzmap.pkg.openapi.ExternalActivity;
import com.uzmap.pkg.openapi.Html5EventListener;
import com.uzmap.pkg.openapi.WebViewProvider;
import org.apache.log4j.Logger;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import app.BaseActivity;
import data.SharedPreferencesUtils;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.StringUtil;

/**
 * 替代 Framgnt 的APIClioud 目前有福利模块
 * 利模块
 9c2ef9d8bc50c0dadeacc5651aac9882
 */
public class APICloud4Activity extends ExternalActivity {
    private String mToken, mId;
    private boolean mActivityState = true;
    private Logger mLogger;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.eE("","APICloudActivity");
        EventBus.getDefault().register(this);
        mId = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mUseridDB, "");
        mToken = (String) SharedPreferencesUtils.getParam(APICloud4Activity.this, HomeUtil.mTokenDB, "");
        mLogger = Logger.getLogger(BaseActivity.class);
        addHtml5EventListener(new Html5EventListener("APICloud4Message") {
            @Override
            public void onReceive(WebViewProvider webViewProvider, Object o) {
                LogUtil.eE("","........addHtml5EventListener.......收到apiCloud的信息....o"+o);
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String type = jsonObject.getString("type");
                    if(type.equals("keyback")){
                        msgHome("keyback");
                    }else if(type.equals("bottom")){
                        String msg = jsonObject.getString("msg");
                        msgHome(msg);
                    }else if(type.equals("top")){
                        String msg = jsonObject.getString("msg");
                        apiCloudOnClick(msg);
                    }else if(type.equals("middle")){
                            clickWelfare();
                    }else if(type.equals("webView")){
                        String title = jsonObject.getString("title");
                        String url = jsonObject.getString("url");
                        JumpActivityUtils.getInstance().jumpWebView(APICloud4Activity.this,url,title);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllHtml5EventListener();
        EventBus.getDefault().unregister(this);
    }

    /*** 订阅事件FirstEvent*/
    @Subscribe
    public void onEventMainThread(final String string){
          LogUtil.eE("",".....onEventMainThread......1..."+string);
            if(string.equals("Close")){
                if(mActivityState){
                    LogUtil.eE("",".....onEventMainThread.....2...."+string);
                    mActivityState = false;
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                }

            }
    }

    /**
     *发消息给首页处理
     */
    private void msgHome(String msg){
        Intent intent = new Intent(HomeActivity.mAction);
        intent.putExtra("wifi","APICloud4");
        intent.putExtra("msg",msg);
        sendBroadcast(intent);
    }

    /**
     * 处理apiCloud的头部事件
     *
     */
    private void apiCloudOnClick(String msg){
      if(msg.equals("topLeft")){
          JumpActivityUtils.getInstance().jumpApiCloud3(this, StringUtil.getStringUtilNew().getPhshContext(),mId);
          StringUtil.getStringUtilNew().clearPhshData(this,0);
      }else if(msg.equals("topRigty")){
          JumpActivityUtils.getInstance().jumpZXing(this);
      }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode,data);
        LogUtil.eE("","onActivityResult......1");
        switch (requestCode) {
            case RESULT_FIRST_USER:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容
                    String string = bundle.getString("result");
                    HomeUtil.getHemeUtilNew().getSessionId(this, string, mId, mToken);
                }
                break;
        }
    }

    /**
     * 点击了福利
     */
    private void clickWelfare(){
        EventBus.getDefault().post("balance");
    }

}
